﻿namespace EyeOpen.DataAnnotationsSample.Mvc.Controllers
{
	using System;
	using System.Web.Mvc;

	using EyeOpen.Data.DataAnnotations.Nhibernate;
	using EyeOpen.DataAnnotations.Mvc.Models;
	using EyeOpen.DataAnnotationsSample.Data;
	using EyeOpen.DataAnnotationsSample.Data.Nhibernate;
	using EyeOpen.Domain;

	public class HomeController : Controller
	{
		[HttpGet]
		public ActionResult Index()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Save(CustomerViewModel vehicleViewModel)
		{
			if (!ModelState.IsValid)
			{
				return PartialView("Index");
			}

			const string ConnectionString =
				@"Data Source=localhost\SQL2008;Initial Catalog=EyeOpen.DataAnnotations;Integrated Security=True";

			using (var dataAnnotationsConfiguration = new AnnotationsConfiguration(ConnectionString, typeof(Customer)))
			{
				using (IAccountingUnitOfWork uow = new AccountingUnitOfWork(dataAnnotationsConfiguration.OpenSession()))
				{
					uow.CustomerRepository.Add(new Customer("dependency") { Id = Guid.NewGuid(), Name = vehicleViewModel.Name });
					uow.Commit();
				}
			}

			return View("Index");
		}
	}
}
